﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using WebApplication1.Controller;
namespace WebApplication1
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        Class1 dd = new Class1();
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        public void idbenhnhan_TextChanged(object sender, EventArgs e)
        {
                    Class1.patient lst = dd.SelectPatient("SELECT * FROM patient where PatientCode ='" + idbenhnhan.Text + "'");
                    if (lst.PatientID != 0)
                    {
                        tenbenhnhan.Text = lst.PatientName;
                        string cuoi = lst.Birthday.ToString();
                        try
                        {
                            string[] st = cuoi.Split(new Char[] { '/' });
                            sinhngay.Text = st[1];
                            sinhthang.Text = st[0];
                            string nam = st[2].Substring(0, 4);
                            sinhnam.Text = nam;
                            sinhtuoi.Text = (DateTime.Now.Date.Year - Convert.ToInt32(nam)).ToString();
                        }
                        catch { sinhnam.Text = ""; sinhthang.Text = ""; sinhngay.Text = ""; sinhtuoi.Text = ""; }
                        GioiTinhCode.Text = lst.GioiTinhCode;
                        NgheNghiepCode.Text = lst.NgheNghiepCode;
                        HC_DanTocCode.Text = lst.HC_DanTocCode;
                        HC_QuocGiaCode.Text = lst.HC_QuocGiaCode;
                        HC_SoNha.Text = lst.HC_SoNha;
                        HC_Thon.Text = lst.HC_Thon;
                        HC_XaCode.Text = lst.HC_XaCode;
                        HC_HuyenCode.Text = lst.HC_HuyenCode;
                        HC_TinhCode.Text = lst.HC_TinhCode;
                        NoiLamViec.Text = lst.NoiLamViec;
                        NguoiThan.Text = lst.NguoiThan;
                        NguoiThan_Phone.Text = lst.NguoiThan_Phone;
                        CMND.Text = lst.CMND;
                        NoiCapCMND.Text = lst.NoiCapCMND;
                        NgayCapCMND.Text = lst.NgayCapCMND.Date.Day.ToString();
                        textbox32.Text = lst.NgayCapCMND.Date.Month.ToString();
                        textbox33.Text = lst.NgayCapCMND.Date.Year.ToString();
                        LastAccessDate.Text = lst.PatientRemark;
                    }
                    else
                    {
                        tenbenhnhan.Text = "";
                        sinhnam.Text = ""; sinhthang.Text = ""; sinhngay.Text = ""; sinhtuoi.Text = "";
                        GioiTinhCode.Text = "";
                        NgheNghiepCode.Text = "";
                        HC_DanTocCode.Text = "";
                        HC_QuocGiaCode.Text = "";
                        HC_SoNha.Text = "";
                        HC_Thon.Text = "";
                        HC_XaCode.Text = "";
                        HC_HuyenCode.Text = "";
                        HC_TinhCode.Text = "";
                        NoiLamViec.Text = "";
                        NguoiThan.Text = "";
                        NguoiThan_Phone.Text = "";
                        CMND.Text = "";
                        NoiCapCMND.Text = "";
                        NgayCapCMND.Text = "";
                        textbox32.Text = "";
                        textbox33.Text = "";
                        LastAccessDate.Text = "";
                    }
        }

        protected void button_Click(object sender, EventArgs e)
        {

            Class1.patient items = new Class1.patient();
            items = dd.SelectPatient("select * from patient where PatientCode='" + idbenhnhan.Text+"'");
            if (items.PatientID == 0)
            {
                items.PatientCode = idbenhnhan.Text;
                items.PatientName = tenbenhnhan.Text;
                try
                {
                    items.Birthday = Convert.ToDateTime(sinhthang.Text + "-" + sinhngay.Text + "-" + sinhnam.Text);
                    items.NgayCapCMND = Convert.ToDateTime(textbox32.Text + "-" + NgayCapCMND.Text + "-" + textbox33.Text); 
                }
                catch { ; }
                items.GioiTinhCode = GioiTinhCode.Text;
                items.NgheNghiepCode = NgheNghiepCode.Text;
                items.HC_DanTocCode = HC_DanTocCode.Text;
                items.HC_HuyenCode = HC_HuyenCode.Text;
                items.HC_QuocGiaCode = HC_QuocGiaCode.Text;
                items.HC_SoNha = HC_SoNha.Text;
                items.HC_Thon = HC_Thon.Text;
                items.HC_TinhCode = HC_TinhCode.Text;
                items.HC_XaCode = HC_XaCode.Text;
                items.NoiLamViec = NoiLamViec.Text;
                items.NguoiThan = NguoiThan.Text;
                items.CMND = CMND.Text;
                items.NoiCapCMND = NoiCapCMND.Text;
               
                items.PatientRemark = LastAccessDate.Text;
            }
            dd.insertItems(items);
            idbenhnhan_TextChanged(sender, e);
        }
    }
}
